//
//  HRDynamicCommentView.m
//  Hear
//
//  Created by mac on 2017/6/16.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicCommentView.h"
#import "YYLabel.h"
#import "HRDynamicCommentModel.h"
#import "HRDynamicViewController.h"
#import "HRDynamicModel.h"
static CGFloat const commentHeight = 20;
static CGFloat const leftPading = 15;

@interface HRDynamicCommentView ()
@property (nonatomic, strong)YYLabel *moreLabel;
@property (nonatomic, strong)NSMutableArray *commentViews;
@property (nonatomic, strong)YYTextLayout *commentLayout;
@property (nonatomic, strong)NSArray *commentArray;

@end
@implementation HRDynamicCommentView




-(void)setDynamicModel:(HRDynamicModel *)dynamicModel{
    self.commentArray = dynamicModel.comentListArray;
    
    
    
    for (int i = 0; i < 3; i ++) {
        YYLabel *commentLabel = self.commentViews[i];
        if (i >= self.commentArray.count){
            commentLabel.hidden = YES;
        }else{
            
            HRDynamicCommentModel *model = self.commentArray[i];

            commentLabel.hidden = NO;
            
            
            NSString *commentStr = @"";
            if ([model.CommentType intValue] == 2){
                commentStr = [NSString stringWithFormat:@"%@：[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName];
            }else{
                commentStr = [NSString stringWithFormat:@"%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.CommentText];

            }
            
            if (model.ReplyUserLocalId.length){
                if ([model.CommentType intValue] == 2){
                    commentStr = [NSString stringWithFormat:@"%@回复%@：[语音]",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName];
                }else{
                    commentStr = [NSString stringWithFormat:@"%@回复%@：%@",model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName,model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName,model.CommentText];
                }
                
            }

            
            if (commentStr.length > 30){
                commentStr = [commentStr substringToIndex:30];
            }
            
            

            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:commentStr];
            
            [att setColor:CustomColor(160, 160, 160, 1) range:NSMakeRange(0, commentStr.length)];

            if (model.ReplyUserLocalId.length){
                

                if ([model.CommentType intValue] == 2){
                    [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];

                    [att setColor:FirstTextColor range:NSMakeRange((model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length+2, (model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName).length)];
                }else{
                    [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];

                    
                    [att setColor:FirstTextColor range:NSMakeRange((model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length+2, (model.UserRemark.length>0?model.UserRemark:model.ReplyUserNickName).length)];
                }
                
            }else{
                [att setColor:FirstTextColor range:NSMakeRange(0, (model.UserRemark.length>0?model.UserRemark:model.CommentUserNickName).length)];
            }
            
            
            [att setFont:CustomUIFont(13) range:NSMakeRange(0, commentStr.length)];
            
            
            CGFloat commentWidth = [att boundingRectWithSize:CGSizeMake(kFullWidth-leftPading*2, commentHeight) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.width;
            commentLabel.frame = CGRectMake(leftPading, commentHeight*i,commentWidth, commentHeight);


            
            YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(commentLabel.width, 20)];
//            container.maximumNumberOfRows = 1;
            _commentLayout = [YYTextLayout layoutWithContainer:container text:att];
            commentLabel.textLayout = _commentLayout;
  
        }
    }
    
    if (self.commentArray.count > 2){
        self.moreLabel.hidden = NO;
        self.moreLabel.frame = CGRectMake(leftPading, commentHeight*3, 100, commentHeight);
    }else{
        self.moreLabel.hidden = YES;
    }
    
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // CGRectMake(leftPading, commentHeight*i, kFullWidth-leftPading*2, commentHeight)
        
        self.commentViews = [NSMutableArray array];
        
        for (int i = 0; i < 3; i ++) {
            YYLabel *label = [YYLabel new];
            label.textColor = CustomColor(160, 160, 160, 1);
            label.font = CustomUIFont(13);
            label.tag = i;
            label.displaysAsynchronously = YES;
            label.ignoreCommonProperties = YES;
            label.fadeOnAsynchronouslyDisplay = NO;
            label.fadeOnHighlight = NO;
            [self addSubview:label];
            [self.commentViews addObject:label];
//            label.textTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
//                YYLabel *commentLabel = (YYLabel *)containerView;
//                HRDynamicCommentModel *model = _commentArray[commentLabel.tag];
//                HRDynamicViewController *dynamicVC = [HRDynamicViewController new];
//                dynamicVC.currentUserID = model.UserLocalId;
//                dynamicVC.hidesBottomBarWhenPushed = YES;
//                [VIEWCCONTROLER.navigationController pushViewController:dynamicVC animated:YES];
//                
//            };
            
            
        }
        
        YYLabel *moreLabel = [YYLabel new];
        moreLabel.text = @"查看全部";
        moreLabel.font = CustomUIFont(12);
        moreLabel.textColor = CustomColor(160, 160, 160, 1);
        [self addSubview:(self.moreLabel = moreLabel)];
        
        
    }
    return self;
}

+(CGSize)sizeDynamicCommentViewCommnetCount:(NSInteger)commentCount{
    CGSize commentSize = CGSizeZero;

    if (commentCount<3){
        commentSize = CGSizeMake(kFullWidth-leftPading*2, commentHeight*commentCount);
    }else if (commentCount > 2){
        commentSize = CGSizeMake(kFullWidth-leftPading*2, commentHeight*3 +commentHeight);
    }
    
    return commentSize;
}


@end
